import javax.naming.InitialContext;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

public class Clients {
    public static void main(String[] args) {
        clientTest();
    }

    public static void clientTest() {

        try {

            System.out.println("Client ready");
            Socket socket = new Socket(InetAddress.getLocalHost(), 8080);
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            byte[] bs = new byte[1024];
            while (true) {

                int[] Index = new int[28];
                // 读取排序
                int len = is.read(bs);// 读取长度
                String cards = new String(bs, 0, len);// 获取pwd
                System.out.println("pwd:" + cards);
                String[] indexStrings = cards.split(" ");
                for (int i = 0; i < 28; i++) {
                    Index[i] = Integer.parseInt(indexStrings[i]);
                    System.out.print(Index[i] + " ");
                }
                System.out.println();
                // 读取先后手
                len = is.read(bs);
                String Order = new String(bs, 0, len);
                System.out.println(Order);
                // 手牌初始化
                int[] MyLibrary = new int[7];
                if (Order.equals("You are the first")) {
                    for (int i = 0; i < MyLibrary.length; i++) {
                        MyLibrary[i] = Index[i];
                        System.out.print(MyLibrary[i] + " ");
                        // os.write("first ready".getBytes());
                    }
                    System.out.println();
                }
                if (Order.equals("You are the second")) {
                    for (int i = 0; i < MyLibrary.length; i++) {
                        MyLibrary[i] = Index[i + 7];
                        System.out.print(MyLibrary[i] + " ");
                        // os.write("second ready".getBytes());
                    }
                    System.out.println();
                }
                os.write("ready".getBytes());
                len = is.read(bs);
                String str = new String(bs, 0, len);
                if (str.equals("start")) {
                    System.out.println("game start!");
                }

                if (Order.equals("You are the first")) {
                    os.write(("0 " + Order + " 想要出的牌").getBytes());
                    while (true) {
                        // String content = sc.nextLine();//会造成阻塞
                        len = is.read(bs);
                        System.out.println(new String(bs, 0, len));
                    }
                }

            }
        } catch (

        IOException e) {
            e.printStackTrace();
        }
    }

}
